﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Linq;
using System.Text;

namespace BitBox.Framework.Util
{
    #region /* Enumeradores */

    /// <summary>
    /// Enum que indica os possíveis caminhos (Producao, Homol etc)
    /// </summary>
    public enum DBConnectTarget
    {
        Default,
        Desenvolvimento,
        Homologacao,
        Producao
    }

    /// <summary>
    /// Enum que indica as possíveis Bases de Dados
    /// </summary>
    public enum DataBase
    {
        BitBox_KBR
    }
    #endregion

    /// <summary>
    /// Classe Principal
    /// </summary>
    public class DBConnect
    {
        #region /* Atributos Privados */

        private const string _RootKey = "SOFTWARE\\BitBox\\DBConnect\\";
        static Hashtable h = new Hashtable();

        #endregion



        #region /* Métodos Privados */

        #endregion



        #region /* Métodos Públicos */
        
        public DBConnect()
        {
        }

        public static string GetDefaultTarget()
        {
            return GetDefaultTarget(_RootKey);
        }

        public static SqlConnection OpenConnection(DataBase dbName)
        {
            string strCn = GetConnectionString(DBConnectTarget.Default, dbName);
            SqlConnection sqlConn = new SqlConnection(strCn);
            sqlConn.Open();
            return sqlConn;
        }

        public static string GetConnectionString(DataBase dbName)
        {
            if (h[dbName.ToString()] != null)
                return h[dbName.ToString()].ToString();

            string Chave = GetConnectionString(DBConnectTarget.Default, dbName);
            h.Add(dbName.ToString(), Chave);
            return Chave;
        }
        
        public static string GetConnectionString(DBConnectTarget target, DataBase dbName)
        {
            string key = _RootKey;
            string connStr = null;
            RegistryKey hKey = null;

            switch (target)
            {
                case(DBConnectTarget.Default):
                    key += GetDefaultTarget(key);
                    break;
                case DBConnectTarget.Producao:
                    key += "Producao";
                    break;
                case DBConnectTarget.Homologacao:
                    key += "Homologacao";
                    break;
                case DBConnectTarget.Desenvolvimento:
                    key += "Desenvolvimento";
                    break;
            }

            try
            {
                hKey = Registry.LocalMachine.OpenSubKey(key);
                if (hKey == null)
                    throw new ApplicationException(String.Format("Não foi possível abrir a chave 'HKEY_LOCAL_MACHINE\\{0}'", key));

                connStr = (string)hKey.GetValue(dbName.ToString());
            }
            finally
            {
                if (hKey != null)
                    hKey.Close();
            }

            return connStr;
        }

        #endregion



        #region /* Métodos privados */

        private static string GetDefaultTarget(string key)
        {
            RegistryKey hKey = null;
            string keyvalue = "";

            try
            {
                hKey = Registry.LocalMachine.OpenSubKey(key);
                if (hKey == null)
                    throw new ApplicationException(String.Format("Não foi possível abrir a chave 'HKEY_LOCAL_MACHINE\\{0}'", key));

                keyvalue = (string)hKey.GetValue("DefaultTarget", "Desenvolvimento");
            }
            finally
            {
                if (hKey != null)
                    hKey.Close();
            }

            return keyvalue;
        }

        #endregion
    }
}
